<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>客户信息管理页面</title>
    <link rel="stylesheet" href="../bootstrap/css/bootstrap.css">

    <style>
        body {
            margin:0;
            padding:0;
            font-family:sans-serif;
            background:#2C2255;
            background:url(../image/水漫漫.jpg);
            background-repeat: no-repeat;
            background-size:100%;
            font-size: 20px;
            font-family:STXingkai;
            color: #59ec0a;
        }
        a{
            text-decoration:none;
        }
        button{
            margin:10px;
        }
        th{
            width:300px;

        }
        td{
            width:200px;
            height:80px;
        }
    </style>
</head>
<body>
    <h1>客户信息管理页面</h1>
    <div id="customer">
        <table id="customer-table" >
            <button data-toggle="modal" data-target="#myModal" class="btn btn-warning modify"  id="insert-customer" >添加新客户</button>
            店名：<input id="companyName" placeholder="请输入店名">
            地址：<input id="companyAddress"  placeholder="请输入地址">
            <button @click="selCustomer()" class="btn btn-warning">查询客户</button>
            <button @click="delCustomer()" id="delCustomer" class="btn btn-danger">删除客户</button>
            <tr>
                <th ><input id="checkAll" type="checkbox" @click="allCheck"></th>
                <th>客户编号</th>
                <th>客户司机编号</th>
                <th>公司名称</th>
                <th>公司地址</th>
                <th>信用度</th>

                <th>操作</th>
            </tr>
            <tr v-for="customer in list" @click="trCheck(event)">
                <td ><input class="check" type="checkbox" :value="customer.customerNo" @click="isCheckAll"></td>
                <td >{{customer.customerNo}}</td>
                <td >{{customer.customerDriverNo}}</td>
                <td >{{customer.companyName}}</td>
                <td >{{customer.companyAddress}}</td>
                <td >{{customer.companyCredit}}</td>
                <td ><input type="number" style="border:none;background-color:transparent;color: #59ec0a;" v-model="customer.phone"></td>
                <td>
                    <button v-on:click="updCustomer(customer)" data-toggle="modal" data-target="#myModal" class="btn btn-danger modify">修改手机号码</button>
                </td>
            </tr>
        </table>
        <!--            分页区域-->
        <div class="pagination-div">
            <ul class="pagination" id="page-ul" >
                <li class="page-item">
                    <a class="page-link" href="javascript:void(0);" @click="cutPage(1)">首页</a>
                </li>
                <li class="page-item" v-if="page.pageNow!=1">
                    <a class="page-link" href="javascript:void(0);"
                       @click="cutPage(page.pageNow-1)">{{page.pageNow-1}}</a></li>
                <li class="page-item active">
                    <a class="page-link" href="javascript:void(0);">{{page.pageNow}}</a>
                </li>
                <li class="page-item" v-if="page.pageNow!=page.pageTotal">
                    <a class="page-link" href="javascript:void(0);"
                       @click="cutPage(page.pageNow+1)">{{page.pageNow+1}}</a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="javascript:void(0);"
                       @click="cutPage(page.pageTotal)">尾页</a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="javascript:void(0);">共有{{page.count}}条数据</a>
                </li>
                <li class="page-item">
                    <a class="page-link" href="javascript:void(0);">共有{{page.pageTotal}}页</a>
                </li>
            </ul>
        </div>

        <!-- 模态框（Modal） -->
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog"  >
                <div class="modal-content" id="myModal-smile" style=" height: 690px;margin: 0">
                    <div class="modal-body" >
                        <h1>添加客户信息</h1>
                        <form id="customer-form">
                            <div class="form-group">
                                <label for="companyName">店铺名称:</label>
                                <input type="text" class="form-control"  name="companyName"  >
                            </div>

                            <div class="form-group">
                                <label for="companyAddress">店铺地址：</label>
                                <input type="text" class="form-control"  name="companyAddress" >
                            </div>

                            <div class="form-group">
                                <label for="companyCredit">信用等级：</label>
                                <input type="number" class="form-control" id="companyCredit" name="companyCredit" >
                            </div>


                            <div class="form-group">
                                <label for="phone">联系方式：</label>
                                <input type="number" class="form-control" id="phone" name="phone">
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" id="refresh" class="btn btn-default" @click="close()" data-dismiss="modal">提交并关闭</button>
                    </div>
                </div>
            </div>
        </div>
    </div>

<!--引入js文件-->
    <script type="text/javascript" src="../css/jquery-3.4.1.js"></script>
    <script src="../bootstrap/js/bootstrap.js" ></script>
    <script src="../vue/vue.js" charset="utf-8"></script>
    <script type="text/javascript" src="../css/my.js"></script>

    <script>
        let customerTabVue

        // 查询客户 需要接受一个当前页参数
        function selCustomer(pageNow,companyName,companyAddress) {
            let map = [];
            $.ajax({
                type: "post",
                url: "../CustomerController/selCustomer",
                dataType: "json",
                data:{pageNow:pageNow, pageCount:5,companyName:companyName,companyAddress:companyAddress},
                async: false,
                success: function (data) {
                    map = data;
                }
            });
            return map;
        }
        /*创建一个map装查询到的第一页的数据*/
        let map = selCustomer(1,null,null);
        //加载分页数据
        customerTabVue = new Vue({
            el: '#customer',
            data: {
                list: map.customerList,// 展示表格
                page: map, // 用于分页
                customer: {}
            },
            methods: {
                //切换页方法 参数为当前按钮所代表的对应页
                cutPage: function (pageNow) {
                    let companyAddress = $("#companyAddress").val();
                    let companyName = $("#companyName").val();
                    //查询要切换页的数据。把结果从新给表格赋值
                    customerTabVue.list = selCustomer(pageNow,companyName,companyAddress).customerList;
                    //把分页的vue重新赋值。
                    customerTabVue.page.pageNow = pageNow;
                },
                show:function (customer) {
                    this.customer = customer;
                },
                //模态框关闭提交修改
                close:function () {
                    $.ajax({
                        type:"post",
                        url:"../CustomerController/addCustomer",
                        data:$("#customer-form").serialize(),
                        dataType:"json",
                        success:function (json) {
                            alert(json.msg);
                            window.location.reload();
                        }
                    })
                },
                //条件查询重新调用查询方法
                selCustomer:function(){
                    //获取两个input框的数据
                    let companyAddress = $("#companyAddress").val();
                    let companyName = $("#companyName").val();
                    customerTabVue.list = selCustomer(1,companyName,companyAddress).customerList;
                    customerTabVue.page = selCustomer(1,companyName,companyAddress);
                    console.log(customerTabVue.page);
                },
                allCheck:function () {
                    eventHub.$emit("allCheck",null);
                },
                isCheckAll:function () {
                    eventHub.$emit("isCheckAll",null);
                },
                trCheck:function (event) {
                    eventHub.$emit("trCheck",event);
                },
                delCustomer:function () {
                    let cusIds = '';
                    //获取被选中的复选框的id值 each()遍历
                    $(".check").each(function () {
                        //对所有符合该选择器的元素进行操作
                        //判断该复选框是否选中
                        if(this.checked){
                            cusIds+=this.value + ",";
                            if(cusIds=="") return;
                            $.ajax({
                                type: "post",
                                url: "../CustomerController/delCustomer",
                                data: {cusIds: cusIds},
                                dataType: "json",
                                success: function (data) {
                                    alert(data.msg);
                                    window.location.reload();
                                }
                            });
                        }
                    });
                },
                updCustomer:function (customer) {
                    $.ajax({
                        type: "post",
                        url: "../CustomerController/updCustomer",
                        data: customer,
                        dataType: "json",
                        success: function (data) {
                            alert(data.msg);
                            window.location.reload();
                        }
                    });
                }
            }
        });

    </script>
</body>
</html>